package controllers

import (
	"encoding/json"
	beego "github.com/beego/beego/v2/server/web"
	"novel-mng/cache"
	"novel-mng/models"
	"strconv"
	"time"
)

type NovelController struct {
	beego.Controller
}
type Res struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func (n *NovelController) Add() {

	img := n.GetString("img")
	title := n.GetString("title")
	author := n.GetString("author")
	status := n.GetString("status")
	content := n.GetString("content")
	state := n.GetString("state")
	novel := models.Novel{
		Img:     img,
		Title:   title,
		Author:  author,
		Status:  status,
		Content: content,
		State:   state,
	}

	err := models.DB.Create(&novel).Error
	if img == "" || title == "" || author == "" || status == "" || content == "" || state == "" {

		n.Data["json"] = Res{
			Code: 0,
			Msg:  "参数不能为空",
		}
		n.ServeJSON()
		return
	}
	if err != nil {
		n.Data["json"] = Res{
			Code: 0,
			Msg:  "添加失败",
		}
		n.ServeJSON()
		return
	} else {
		n.Data["json"] = Res{
			Code: 200,
			Msg:  "添加成功",
		}
		n.ServeJSON()
	}
}
func (n *NovelController) Upload() {

	file, heard, err := n.GetFile("img")
	if err != nil {
		n.Data["json"] = Res{
			Code: 0,
			Msg:  "文件获取失败",
		}
		n.ServeJSON()
		return
	}
	if heard.Size > 2*1024*1024 {
		n.Data["json"] = Res{
			Code: 0,
			Msg:  "文件过大",
		}
		n.ServeJSON()
		return
	}
	defer file.Close()
	path := "static/img/" + heard.Filename
	err = n.SaveToFile("img", path)
	if err != nil {
		n.Data["json"] = Res{
			Code: 0,
			Msg:  "文件上传失败",
		}
		n.ServeJSON()
		return
	} else {
		Url := "http://127.0.0.1:8080/" + path
		n.Data["json"] = Res{
			Code: 200,
			Msg:  "文件上传成功",
			Data: Url,
		}
		n.ServeJSON()
	}
}
func (n *NovelController) List() {
	dataListKey := "data_list"
	cacheJson, _ := cache.Rdb.Get(cache.Ctx, dataListKey).Result()
	var novel []models.Novel
	if cacheJson == "" {
		models.DB.Model(&models.Novel{}).Find(&novel)
		jsonData, _ := json.Marshal(novel)
		cache.Rdb.Set(cache.Ctx, dataListKey, jsonData, time.Hour*2)
	} else {
		err := json.Unmarshal([]byte(cacheJson), &novel)
		if err != nil {
			n.Data["json"] = Res{
				Code: 0,
				Msg:  "缓存数据获取失败",
			}
			n.ServeJSON()
			return
		}
		n.Data["json"] = map[string]interface{}{
			"Code": 200,
			"Msg":  "缓存数据获取成功",
			"Data": novel,
		}
		n.ServeJSON()
		return

	}
}
func (n *NovelController) Del() {

	id, _ := n.GetInt("id")
	err := models.DB.Delete(&models.Novel{}, id).Error
	if err != nil {
		n.Data["json"] = Res{
			Code: 0,
			Msg:  "删除失败",
		}
		n.ServeJSON()
		return
	} else {
		n.Data["json"] = Res{
			Code: 200,
			Msg:  "删除成功",
		}
		n.ServeJSON()
	}
}
func (n *NovelController) Login() {

	id, _ := n.GetInt("id")
	dataListKey := "data_list_" + strconv.Itoa(id)
	cacheJson, _ := cache.Rdb.Get(cache.Ctx, dataListKey).Result()
	var novel []models.Novel
	if cacheJson == "" {
		models.DB.Model(&models.Novel{}).Where("id=?", id).First(&novel)
		jsonData, _ := json.Marshal(&novel)
		cache.Rdb.Set(cache.Ctx, dataListKey, jsonData, time.Hour*2)
	} else {
		err := json.Unmarshal([]byte(cacheJson), &novel)
		if err != nil {
			n.Data["json"] = Res{
				Code: 0,
				Msg:  "详情缓存失败",
			}
			n.ServeJSON()
			return
		} else {
			n.Data["json"] = Res{
				Code: 200,
				Msg:  "详情缓存成功",
				Data: novel,
			}
			n.ServeJSON()
		}
	}
}
func (n *NovelController) Lists() {

	page, _ := n.GetInt("page", 1)
	size, _ := n.GetInt("size", 3)
	offset := (page - 1) * size
	query := models.DB.Model(&models.Novel{})
	var count int64
	query.Count(&count)
	var novel []models.Novel
	err := query.Offset(offset).Limit(size).Find(&novel).Error
	if err != nil {
		n.Data["json"] = Res{
			Code: 0,
			Msg:  "查询失败",
		}
		n.ServeJSON()
		return
	} else {
		PageData := make(map[string]interface{})
		PageData["count"] = count
		PageData["novel"] = novel
		n.Data["json"] = Res{
			Code: 200,
			Msg:  "查询成功",
			Data: PageData,
		}
		n.ServeJSON()
	}
}
